package com.cn.yugan.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.WindowManager;

import com.cn.yugan.Cons.LoginConstants;
import com.cn.yugan.Services.NoticeMessageService;
import com.cn.yugan.mobile.R;
import com.cn.yugan.Utils.IntentUtils;
import com.umeng.analytics.MobclickAgent;

import org.litepal.tablemanager.Connector;

import java.io.File;

/**
 * Created by Rover on 2016/7/17 .
 * Email 357319517@qq.com
 * <p/>
 * 该页面为登录前的载入页面
 * 用于前期耗时载入、看板娘的宣传以及广告等。
 */
public class SplashActivity extends AppCompatActivity {
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);


        initactionbar();

        initdb();
        initUmeng();

        setupServices();
        startActivity();
    }

    /**
     * 开启服务
     */
    private void setupServices() {
        //消息中心服务
        startService(new Intent(SplashActivity.this, NoticeMessageService.class));
    }

    /**
     * 获取登录状态 并选择 进入状态
     */
    private void startActivity() {
        sp = getSharedPreferences("Session", MODE_PRIVATE);
        if (sp.getString("uid", null) == null) {
            IntentUtils.startActivityForDelayAndFinish(SplashActivity.this, LoginAcitivity.class, 4000);
        } else {
            Intent intent = new Intent(SplashActivity.this, IndexActivity.class);
            intent.putExtra("LoginMode", LoginConstants.LOGIN_NORMAL);
            startActivity(intent);
            SplashActivity.this.finish();
        }
    }

    /**
     * UMeng启动
     */
    private void initUmeng() {
        //开启测试模式
        MobclickAgent.setDebugMode(true);
    }

    /**
     * 加载数据库
     */
    private void initdb() {
        SQLiteDatabase sldb = Connector.getDatabase();
    }

    /**
     * 初始化Actionbar
     */
    private void initactionbar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }

    /**
     * back键无法退出
     */
   
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
